using System;
using System.Web.UI.WebControls;
using System.Xml.Linq;

namespace Code5Systems.FormMaster.Business
{

	public partial class FormController
	{
		
#region Validator Control Related Methods
		
		/// -----------------------------------------------------------------------------
		/// <summary>
		/// Adds a Validation Control of the Given type to the given FormControlInfo Object
		/// </summary>
		/// <param name="defXmlDoc">The Current Working Form XmlDocument</param>
		/// <param name="oFormControlInfo">The FormControlInfo Object to add the Valiator to</param>
		/// <param name="type">The Type of Validator Control to add</param>
		/// <remarks>
		/// Adds the Validator with Default Values
		/// </remarks>
		/// <history>
		/// 	[steve]	4/30/2005	Created
		/// </history>
		/// -----------------------------------------------------------------------------
		public FormControlInfo AddValidatorControl(XDocument xDoc, FormControlInfo oFormControlInfo, ControlType type)
		{
			FormControlInfo objValidatorControlInfo = null;
			XElement xControl = default(XElement);
			XElement xValidatorControl = default(XElement);
			XElement xValidatorsControl = default(XElement);
			
			xControl = this.GetControlElement(xDoc, oFormControlInfo.TableRow, oFormControlInfo.TableCol);
			
			if (xControl != null)
			{
				xValidatorControl = this.GetValidatorControlElement(xControl, type);
				if (xValidatorControl == null)
				{
					//xValidatorsControl = Utility.AddElement(xDoc, xControl, "Validators", "")
					xValidatorsControl = new XElement("Validators");
					xControl.Add(xValidatorsControl);
					//xValidatorControl = Utility.AddElement(xDoc, xValidatorsControl, "Control", "")
					xValidatorControl = new XElement("Control");
					xValidatorsControl.Add(xValidatorControl);
					
					//Utility.AddAttribute(xDoc, xValidatorControl, "Type", type.ToString())
					xValidatorControl.Add(new XAttribute("Type", type.ToString()));
					
					//Utility.AddAttribute(xDoc, xValidatorControl, "Label", "")
					xValidatorControl.Add(new XElement("Label"));
					
					//Utility.AddAttribute(xDoc, xValidatorControl, "TableCol", oFormControlInfo.TableCol.ToString())
					xValidatorControl.Add(new XAttribute("TableCol", oFormControlInfo.TableCol.ToString()));
					
					//Utility.AddAttribute(xDoc, xValidatorControl, "TableRow", oFormControlInfo.TableRow.ToString())
					xValidatorControl.Add(new XAttribute("TableRow", oFormControlInfo.TableRow.ToString()));
					
					//Utility.AddAttribute(xDoc, xValidatorControl, "ControlToValidate", oFormControlInfo.ID)
					//Utility.AddAttribute(xDoc, xValidatorControl, "Display", "Dynamic")
					//Utility.AddAttribute(xDoc, xValidatorControl, "ErrorMessage", "A value is required")
					//Utility.AddAttribute(xDoc, xValidatorControl, "CssClass", "NormalRed")
					
					switch (type)
					{
						case ControlType.RequiredFieldValidator:
							//Utility.AddAttribute(xDoc, xValidatorControl, "ID", "rfv_" & Guid.NewGuid().ToString().Replace("-", ""))
							xValidatorControl.Add(new XAttribute("ID", "rfv_" + Guid.NewGuid().ToString().Replace("-", "")));
							xValidatorControl.Add(new XAttribute("ControlToValidate", oFormControlInfo.ID));
							xValidatorControl.Add(new XAttribute("Display", "Dynamic"));
							xValidatorControl.Add(new XAttribute("ErrorMessage", "A value is required"));
							xValidatorControl.Add(new XAttribute("CssClass", "NormalRed"));
							oFormControlInfo.Required = true;
							break;
							
						case ControlType.ChkListValidator:
							//Utility.AddAttribute(xDoc, xValidatorControl, "ID", "clv_" & Guid.NewGuid().ToString().Replace("-", ""))
							xValidatorControl.Add(new XAttribute("ID", "clv_" + Guid.NewGuid().ToString().Replace("-", "")));
							xValidatorControl.Add(new XAttribute("ControlToValidate", oFormControlInfo.ID));
							xValidatorControl.Add(new XAttribute("Display", "Dynamic"));
							xValidatorControl.Add(new XAttribute("ErrorMessage", "A value is required"));
							xValidatorControl.Add(new XAttribute("CssClass", "NormalRed"));
							//Utility.AddAttribute(xDoc, xValidatorControl, "MinChecked", "1")
							xValidatorControl.Add(new XAttribute("MinChecked", "1"));
							//Utility.AddAttribute(xDoc, xValidatorControl, "MaxChecked", "0")
							xValidatorControl.Add(new XAttribute("MaxChecked", "0"));
							oFormControlInfo.Required = true;
							break;
							
						case ControlType.RegularExpressionValidator:
							//Utility.AddAttribute(xDoc, xValidatorControl, "ID", "rev_" & Guid.NewGuid().ToString().Replace("-", ""))
							xValidatorControl.Add(new XAttribute("ID", "rev_" + Guid.NewGuid().ToString().Replace("-", "")));
							xValidatorControl.Add(new XAttribute("ControlToValidate", oFormControlInfo.ID));
							xValidatorControl.Add(new XAttribute("Display", "Dynamic"));
							xValidatorControl.Add(new XAttribute("ErrorMessage", "A value is required"));
							xValidatorControl.Add(new XAttribute("CssClass", "NormalRed"));
							//Utility.AddAttribute(xDoc, xValidatorControl, "ValidationExpression", "")
							xValidatorControl.Add(new XAttribute("ValidationExpression", ""));
							break;
							
						case ControlType.CompareValidator:
							//Utility.AddAttribute(xDoc, xValidatorControl, "ID", "cv_" & Guid.NewGuid().ToString().Replace("-", ""))
							xValidatorControl.Add(new XAttribute("ID", "cv_" + Guid.NewGuid().ToString().Replace("-", "")));
							xValidatorControl.Add(new XAttribute("ControlToValidate", oFormControlInfo.ID));
							xValidatorControl.Add(new XAttribute("Display", "Dynamic"));
							xValidatorControl.Add(new XAttribute("ErrorMessage", "A value is required"));
							xValidatorControl.Add(new XAttribute("CssClass", "NormalRed"));
							
							//Utility.AddAttribute(xDoc, xValidatorControl, "Operator", ValidationCompareOperator.DataTypeCheck.ToString())
							xValidatorControl.Add(new XAttribute("Operator", ValidationCompareOperator.DataTypeCheck.ToString()));
							//Utility.AddAttribute(xDoc, xValidatorControl, "ControlToCompare", "")
							xValidatorControl.Add(new XAttribute("ControlToCompare", ""));
							//Utility.AddAttribute(xDoc, xValidatorControl, "ValDataType", ValidationDataType.String.ToString())
							xValidatorControl.Add(new XAttribute("ValDataType", ValidationDataType.String.ToString()));
							//Utility.AddAttribute(xDoc, xValidatorControl, "ValueToCompare", "")
							xValidatorControl.Add(new XAttribute("ValueToCompare", ""));
							break;
							
					}
					
					objValidatorControlInfo = GetFormControlInfo(xValidatorControl);
					
					oFormControlInfo.Validators.Add(objValidatorControlInfo);
					
					UpdateControlValues(xDoc, oFormControlInfo);
					
				}
			}
			
			return objValidatorControlInfo;
			
		}
		
		/// -----------------------------------------------------------------------------
		/// <summary>
		///
		/// </summary>
		/// <param name="defXmlDoc">The Current Working Form XmlDocument</param>
		/// <param name="oFormControlInfo">The FormControlInfo Object for the Current Cell</param>
		/// <param name="type">The Type of Validator Control to Delete</param>
		/// <remarks>
		/// There can only be one validator of a specific type for each control.
		/// </remarks>
		/// <history>
		/// 	[steve]	4/30/2005	Created
		/// </history>
		/// -----------------------------------------------------------------------------
		public void DeleteValidatorControl(XDocument xDoc, FormControlInfo oFormControlInfo, ControlType type)
		{
			//Dim oControlNode As XmlNode
			XElement xControl = default(XElement);
			//Dim oValidatorNode As XmlNode
			XElement xValidator = default(XElement);
			
			//oControlNode = defXmlDoc.SelectSingleNode("//Controls/Control[@TableRow=" & oFormControlInfo.TableRow & "][@TableCol=" & oFormControlInfo.TableCol & "]")
			xControl = this.GetControlElement(xDoc, oFormControlInfo.TableRow, oFormControlInfo.TableCol);
			if (xControl != null)
			{
				//oValidatorNode = oControlNode.SelectSingleNode("Validators/Control[@Type='" & type.ToString() & "']")
				xValidator = this.GetValidatorControlElement(xControl, type);
				if (xValidator != null)
				{
					//oValidatorNode.ParentNode.RemoveChild(oValidatorNode)
					xValidator.Remove();
					if (type == ControlType.RequiredFieldValidator)
					{
						oFormControlInfo.Required = false;
						this.UpdateTempControlValues(xDoc, oFormControlInfo);
					}
				}
			}
		}
		
		/// -----------------------------------------------------------------------------
		/// <summary>
		/// Gets the a FormControlInfo Object for a Validation Control
		/// </summary>
		/// <param name="defXmlDoc">The Current Working Form XmlDocument</param>
		/// <param name="oFormControlInfo">The FormControlInfo Object for the Current Cell</param>
		/// <param name="type">The Type of Validator Control to get</param>
		/// <remarks>
		/// There can only be one validator of a specific type for each control.
		/// </remarks>
		/// <history>
		/// 	[steve]	4/30/2005	Created
		/// </history>
		/// -----------------------------------------------------------------------------
		public FormControlInfo GetValidatorControlInfo(XDocument xDoc, FormControlInfo oFormControlInfo, ControlType type)
		{
			FormControlInfo oValidatorControlInfo = null;
			XElement xControl = default(XElement);
			XElement xValidatorControl = default(XElement);
			
			xControl = this.GetControlElement(xDoc, oFormControlInfo.TableRow, oFormControlInfo.TableCol);
			
			if (xControl != null)
			{
				xValidatorControl = this.GetValidatorControlElement(xControl, type);
				if (xValidatorControl != null)
				{
					oValidatorControlInfo = this.GetFormControlInfo(xValidatorControl);
				}
			}
			
			return oValidatorControlInfo;
			
		}
		
		/// -----------------------------------------------------------------------------
		/// <summary>
		/// Sets the Cell postion of a validator control Used when a control is moved.
		/// </summary>
		/// <param name="controlNode">The XmlElement Node for the Current Cell</param>
		/// <param name="row">The Target Row to move to</param>
		/// <param name="col">The Target Column to move to</param>
		/// <remarks>
		/// </remarks>
		/// <history>
		/// 	[steve]	4/30/2005	Created
		/// </history>
		/// -----------------------------------------------------------------------------
		public void SetValidatorControlsPosition(XElement xControl, ControlLoaction oMoveTarget)
		{
			XAttribute xAttr = default(XAttribute);
			
			foreach (XElement validator in xControl.Elements("Validators").Elements("Control"))
			{
				xAttr = validator.Attribute("Page");
				if (xAttr != null)
				{
					xAttr.Value = oMoveTarget.Page.ToString();
				}
				xAttr = validator.Attribute("TableRow");
				if (xAttr != null)
				{
					xAttr.Value = oMoveTarget.Row.ToString();
				}
				xAttr = validator.Attribute("TableCol");
				if (xAttr != null)
				{
					xAttr.Value = oMoveTarget.Col.ToString();
				}
			}
		}
		
#endregion
		
	}
	
}

